<?php

/**
 * This is the model class for table "prefix_user".
 *
 * The followings are the available columns in table 'prefix_user':
 * @property string $user_id
 * @property string $user_login
 * @property string $user_password
 * @property string $user_mail
 * @property double $user_skill
 * @property string $user_date_register
 * @property string $user_date_activate
 * @property string $user_date_comment_last
 * @property string $user_ip_register
 * @property double $user_rating
 * @property string $user_count_vote
 * @property integer $user_activate
 * @property string $user_activate_key
 * @property string $user_profile_name
 * @property string $user_profile_sex
 * @property string $user_profile_country
 * @property string $user_profile_region
 * @property string $user_profile_city
 * @property string $user_profile_birthday
 * @property string $user_profile_site
 * @property string $user_profile_site_name
 * @property string $user_profile_icq
 * @property string $user_profile_about
 * @property string $user_profile_date
 * @property string $user_profile_avatar
 * @property string $user_profile_foto
 * @property integer $user_settings_notice_new_topic
 * @property integer $user_settings_notice_new_comment
 * @property integer $user_settings_notice_new_talk
 * @property integer $user_settings_notice_reply_comment
 * @property integer $user_settings_notice_new_friend
 * @property string $lsProfileUrl
 * @property string $avatarUrl
 * @property array(Blog) $blogs
 * @property CityUser $cityUser
 * @property array(Comment) $comments
 * @property CountryUser $countryUser
 * @property array(Favourite) $favourites
 * @property array(Friend) $friends
 * @property array(Invite) $invites
 * @property array(Reminder) $reminders
 * @property array(Session) $sessions
 * @property array(Talk) $talks
 * @property array(TalkBlacklist) $talkBlacklists
 * @property array(Topic) $topicsRead
 * @property array(Topic) $topics
 * @property array(TopicTag) $topicTags
 * @property UserAdministrator $userAdministrator
 * @property array(Vote) $votes
 * @property Blog $personalBlog
 */
class User extends LsActiveRecord
{	
	/**
	 * @var User
	 */
	protected static $_me;

	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return Config::Get('db.table.user');
	}
	
	public function getEntity(){
		if($entity = $this->entityName()){
			$attrs = $this->getAttributes();
			$ua = $this->getRelated('userAdministrator');
			$attrs += array('user_is_administrator' => (bool) $ua);
			return Yii::E()->GetEntity($entity, $attrs + array('ar' => $this));
		}
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_login, user_password, user_date_register, user_ip_register', 'required'),
			array('user_activate, user_settings_notice_new_topic, user_settings_notice_new_comment, user_settings_notice_new_talk, user_settings_notice_reply_comment, user_settings_notice_new_friend', 'numerical', 'integerOnly'=>true),
			array('user_skill, user_rating', 'numerical'),
			array('user_login, user_profile_country, user_profile_region, user_profile_city', 'length', 'max'=>30),
			array('user_password, user_mail, user_profile_name, user_profile_site_name', 'length', 'max'=>50),
			array('user_ip_register, user_profile_icq', 'length', 'max'=>20),
			array('user_count_vote', 'length', 'max'=>11),
			array('user_activate_key', 'length', 'max'=>32),
			array('user_profile_sex', 'length', 'max'=>5),
			array('user_profile_site', 'length', 'max'=>200),
			array('user_profile_avatar, user_profile_foto', 'length', 'max'=>250),
			array('user_date_activate, user_date_comment_last, user_profile_birthday, user_profile_about, user_profile_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, user_login, user_password, user_mail, user_skill, user_date_register, user_date_activate, user_date_comment_last, user_ip_register, user_rating, user_count_vote, user_activate, user_activate_key, user_profile_name, user_profile_sex, user_profile_country, user_profile_region, user_profile_city, user_profile_birthday, user_profile_site, user_profile_site_name, user_profile_icq, user_profile_about, user_profile_date, user_profile_avatar, user_profile_foto, user_settings_notice_new_topic, user_settings_notice_new_comment, user_settings_notice_new_talk, user_settings_notice_reply_comment, user_settings_notice_new_friend', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		$aTables = Config::Get('db.table');
		return array(
			'blogs' => array(self::MANY_MANY, 'Blog', $aTables['blog_user'].'(blog_id, user_id)'),
			'personalBlog' => array(self::HAS_ONE, 'Blog', 'user_owner_id', 'condition' => '{%child}.blog_type="personal"'),
			'ownedBlogs' => array(self::HAS_MANY, 'Blog', 'user_owner_id', 'condition' => '{%child}.blog_type!="personal"'),
			'votedBlogs' => array(self::BELONGS_TO, 'Blog', 'target_id', 'through'=>'blogVotes'),
			'commentedBlogs' => array(self::BELONGS_TO, 'Blog', 'blog_id', 'through'=>'commentedTopics',),
			'topicVotedBlogs' => array(self::BELONGS_TO, 'Blog', 'blog_id', 'through'=>'votedTopics',),
			'commentVotedBlogs' => array(self::BELONGS_TO, 'Blog', 'blog_id', 'through'=>'commentVotedTopics',),
			
			'cityUser' => array(self::BELONGS_TO, 'CityUser', 'user_id'),
			'city' => array(self::BELONGS_TO, 'City', 'city_id', 'through'=>'cityUser'),
			
			'comments' => array(self::HAS_MANY, 'Comment', 'user_id'),
			'countComments' => array(self::HAS_MANY, 'Comment', 'user_id'),
			'topicComments' => array(self::HAS_MANY, 'Comment', 'user_id', 'condition' => '{%child}.target_type="topic"'),
			'favouritedComments' => array(self::BELONGS_TO, 'Comment', 'target_id', 'through'=>'commentFavourites'),
			'votedComments' => array(self::BELONGS_TO, 'Comment', 'target_id', 'through'=>'commentVotes'),
			'votedTopicComments' => array(self::BELONGS_TO, 'Comment', 'target_id', 'through'=>'commentVotes', 'condition' => '{%child}.target_type="topic"'),
			
			'countryUser' => array(self::HAS_ONE, 'CountryUser', 'user_id'),
			'country' => array(self::BELONGS_TO, 'Country', 'country_id', 'through'=>'countryUser'),
			
			'countFavourites' => array(self::STAT, 'Favourite', 'user_id'),
			'favourites' => array(self::HAS_MANY, 'Favourite', 'user_id'),
			'topicFavourites' => array(self::HAS_MANY, 'Favourite', 'user_id', 'condition' => '{%child}.target_type="topic"'),
			'commentFavourites' => array(self::HAS_MANY, 'Favourite', 'user_id', 'condition' => '{%child}.target_type="comment"'),
			'talkFavourites' => array(self::HAS_MANY, 'Favourite', 'user_id', 'condition' => '{%child}.target_type="talk"'),
		
			'friendsTo' => array(self::HAS_MANY, 'Friend', 'user_to'),
			'friendToUsers' => array(self::BELONGS_TO, 'User', 'user_from', 'through'=>'friendsTo'),
			'friendsFrom' => array(self::HAS_MANY, 'Friend', 'user_from'),
			'friendFromUsers' => array(self::BELONGS_TO, 'User', 'user_to', 'through'=>'friendsFrom'),
			'votedUsers' => array(self::BELONGS_TO, 'User', 'target_id', 'through'=>'userVotes'),
			'invitedUsers' => array(self::HAS_MANY, 'User', 'user_to_id', 'through'=>'invites'),
			'invitedByUser' => array(self::BELONGS_TO, 'User', 'user_from_id', 'through'=>'invite'),
			
			'invite' => array(self::HAS_ONE, 'Invite', 'user_to_id'),
			'invites' => array(self::HAS_MANY, 'Invite', 'user_from_id'),
			'reminders' => array(self::HAS_MANY, 'Reminder', 'user_id'),
			'sessions' => array(self::HAS_MANY, 'Session', 'user_id'),
		
			'talks' => array(self::MANY_MANY, 'Talk', $aTables['talk_user'].'(talk_id, user_id)'),
			'favouritedTalks' => array(self::BELONGS_TO, 'Talk', 'target_id', 'through'=>'talkFavourites'),
			'commentedTalks' => array(self::BELONGS_TO, 'Talk', 'target_id', 'through'=>'talkComments'),
			'talkBlacklists' => array(self::HAS_MANY, 'TalkBlacklist', 'user_id'),
			
			'readTopics' => array(self::MANY_MANY, 'Topic', $aTables['topic_read'].'(topic_id, user_id)'),
			'topics' => array(self::HAS_MANY, 'Topic', 'user_id'),
			'countTopics' => array(self::STAT, 'Topic', 'user_id'),
			'commentedTopics' => array(self::BELONGS_TO, 'Topic', 'target_id', 'through'=>'topicComments'),
			'votedTopics' => array(self::BELONGS_TO, 'Topic', 'target_id', 'through'=>'topicVotes'),
			'favouritedTopics' => array(self::BELONGS_TO, 'Topic', 'target_id', 'through'=>'topicFavourites'),
			'commentVotedTopics' => array(self::BELONGS_TO, 'Topic', 'target_id', 'through'=>'votedTopicComments'),
			
			'topicTags' => array(self::HAS_MANY, 'TopicTag', 'user_id'),
			'userAdministrator' => array(self::HAS_ONE, 'UserAdministrator', 'user_id'),
			'votes' => array(self::HAS_MANY, 'Vote', 'user_voter_id'),
			'topicVotes' => array(self::HAS_MANY, 'Vote', 'user_voter_id', 'condition' => '{%child}.target_type="topic"'),
			'blogVotes' => array(self::HAS_MANY, 'Vote', 'user_voter_id', 'condition' => '{%child}.target_type="blog"'),
			'userVotes' => array(self::HAS_MANY, 'Vote', 'user_voter_id', 'condition' => '{%child}.target_type="user"'),
			'commentVotes' => array(self::HAS_MANY, 'Vote', 'user_voter_id', 'condition' => '{%child}.target_type="comment"'),
		);
	}

	
	public function validatePassword($password){
		return $this->hashPassword($password) == $this->user_password;
	}
	
	public function hashPassword($password){
		return md5($password);
	}
	
	public function getAvatarUrl($size=24){
		if(!preg_match('#^http:#', $this->user_profile_avatar)){
			return Config::Get('path.static.skin')."/images/avatar_{$size}x{$size}.jpg";
		}
		return strtr($this->user_profile_avatar, array('100x100' => "{$size}x{$size}"));
	}
	
	public function getLsProfileUrl(){
		return Router::GetPath('profile/'.urldecode($this->user_login));
	}
	
	public function getLink($htmlOptions=array()){
		return CHtml::link($this->user_login, $this->getLsProfileUrl(), $htmlOptions + array(
			'title' => $this->user_profile_name
		));
	}
	
	/**
	 * @return User
	 */
	static public function me(){
		$iUserId = Yii::app()->user->id;
		if(self::$_me){
			$oMe = self::$_me;
			if($oMe->user_id == $iUserId){
				return $oMe;
			}
		}
		if($iUserId){
			self::$_me = self::model()->findByPk($iUserId);
			return self::$_me;
		}
	}
	
	
	public function defScope(){
		return array(
			'alias' => $this->tableName(),
			'with' => array('userAdministrator'),
		);
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'user_login' => 'User Login',
			'user_password' => 'User Password',
			'user_mail' => 'User Mail',
			'user_skill' => 'User Skill',
			'user_date_register' => 'User Date Register',
			'user_date_activate' => 'User Date Activate',
			'user_date_comment_last' => 'User Date Comment Last',
			'user_ip_register' => 'User Ip Register',
			'user_rating' => 'User Rating',
			'user_count_vote' => 'User Count Vote',
			'user_activate' => 'User Activate',
			'user_activate_key' => 'User Activate Key',
			'user_profile_name' => 'User Profile Name',
			'user_profile_sex' => 'User Profile Sex',
			'user_profile_country' => 'User Profile Country',
			'user_profile_region' => 'User Profile Region',
			'user_profile_city' => 'User Profile City',
			'user_profile_birthday' => 'User Profile Birthday',
			'user_profile_site' => 'User Profile Site',
			'user_profile_site_name' => 'User Profile Site Name',
			'user_profile_icq' => 'User Profile Icq',
			'user_profile_about' => 'User Profile About',
			'user_profile_date' => 'User Profile Date',
			'user_profile_avatar' => 'User Profile Avatar',
			'user_profile_foto' => 'User Profile Foto',
			'user_settings_notice_new_topic' => 'User Settings Notice New Topic',
			'user_settings_notice_new_comment' => 'User Settings Notice New Comment',
			'user_settings_notice_new_talk' => 'User Settings Notice New Talk',
			'user_settings_notice_reply_comment' => 'User Settings Notice Reply Comment',
			'user_settings_notice_new_friend' => 'User Settings Notice New Friend',
		);
	}

}